package com.tanhua.api;

import com.tanhua.sso.pojo.FocusUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class FocusUserServiceApiImpi implements FocusUserServiceApi{

    @Autowired
    private MongoTemplate mongoTemplate;
    //判断是否关注
    @Override
    public boolean isHasFocus(Long followUserId, Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("followUserId").is(followUserId);
        boolean exists = mongoTemplate.exists(Query.query(criteria), FocusUser.class);
        return exists;
    }

    //存入关注
    @Override
    public void save(FocusUser focusUser) {
        mongoTemplate.save(focusUser);
    }

    //取关
    @Override
    public void delectFocus(Long uid, Long userId) {
        Criteria criteria = Criteria.where("followUserId").is(uid).and("userId").is(userId);
        mongoTemplate.remove(Query.query(criteria),FocusUser.class);
    }

    //我的粉丝
    @Override
    public List<FocusUser> selectAll(Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId);
        List<FocusUser> focusUserList = mongoTemplate.find(Query.query(criteria), FocusUser.class);
        return focusUserList;
    }
}
